﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ProjectRoughWork.Engine.SoundManager;
namespace ProjectRoughWork
{
    class SquadActor : BaseActor
    {
        public enum UnitState { Attacking, Retreating, Regrouping, Moving };

        /// <summary>
        /// Default Constructor to initial data members
        /// </summary>

        public SquadActor() : base()
        {
            maxAccel = 3.0f;
            maxVel = 3.0f;

            canFire = true;
            gunFireCooldownTimer = 0f;

            mType = "SquadActor";
            inCover = false;
            xp = 0;


        }

        /// <summary>
        /// Property for the unit type
        /// </summary>

        public string UnitType
        {
            get { return unitType; }
        }

        /// <summary>
        /// Property for the actor's current AI state
        /// </summary>

        public AIState CurrentAIState
        {
            get { return currentState; }
            set { currentState = value; }
        }

        /// <summary>
        /// Property for the actor's current unit state
        /// </summary>

        public UnitState CurrentUnitState
        {
            get { return unitState; }
            set { unitState = value; }
        }

        /// <summary>
        /// Property for the actor's current health
        /// </summary>

        public float CurrentHealth
        {
            get { return currentHealth; }
            set { currentHealth = value; }
        }

        /// <summary>
        /// Property for the actor's maximum health
        /// </summary>

        public float MaxHealth
        {
            get { return maxHealth; }
            set { maxHealth = value; }
        }

        /// <summary>
        /// Property for the rate of health regeneration
        /// </summary>

        public float HealthRegenRate
        {
            get { return healthRegenRate; }
            set { healthRegenRate = value; }
        }

        /// <summary>
        /// Property for the range of the actor's gun
        /// </summary>

        public float GunRange
        {
            get { return gunRange; }
            set { gunRange = value; }
        }

        /// <summary>
        /// Property for whether the actor is in cover
        /// </summary>

        public bool InCover
        {
            get { return inCover; }
            set { inCover = value; }
        }

        ///<summary>
        /// Property for whether the actor is firing
        ///</summary>
       
        public bool Firing
        {
            get { return firing; }
            set { firing = value; }
        }

        /// <summary>
        /// Property for whether the actor is crouching
        /// </summary>

        public bool Crouching
        {
            get { return crouching; }
            set { crouching = value; }
        }

        /// <summary>
        /// Property for wehther the actor is part of the player squad
        /// </summary>

        public bool Player
        {
            get { return player; }
            set { player = value; }
        }

        /// <summary>
        /// Property for whether the actor is being controlled by the user
        /// </summary>

        public bool User
        {
            get { return user; }
            set { user = value; }
        }

        /// <summary>
        /// Determines whether the actor is dead
        /// </summary>
        /// <returns>True if current health is 0, False if not</returns>

        public bool isDead()
        {
            return (currentHealth == 0);
        }

        /// <summary>
        /// Property for the actor's xp
        /// </summary>

        public int XP 
        {
            get {return xp;}
            set {xp = value;}
        }

        /// <summary>
        /// Property for the model name to be loaded
        /// </summary>
        public string ModelType
        {
            get { return modelName; }
            set { modelName = value; }
        }

        //Method for regenerating health

        public virtual void RegenerateHealth()
        {
            if (currentHealth < maxHealth)
            {
                currentHealth += MathHelper.Clamp(maxHealth - currentHealth, 0, healthRegenRate);
            }
        }

        /// <summary>
        /// Fires the squad actor's gun at the targt given if not in cooldown
        /// </summary>
        /// <param name="target">The intended target to shoot</param>
        /// <returns>Returns true if fired, false if the squad actor didn't fire due to cooldown</returns>

        public virtual bool Shoot(Vector3 target)
        {
            if (canFire)
            {
                canFire = false;

                //Create Projectile here

                return true;
            }
            return false; 
        }

        /// <summary>
        /// Method for dealing damage to an actor
        /// </summary>
        /// <param name="damage">The amount of damage to deal</param>

        public void GotHit(int damage)
        {
            if (currentHealth != 0)
            {
                currentHealth = (int)MathHelper.Clamp(currentHealth - damage, 0, currentHealth - damage);
            }
            SoundManager.GetInstance().playSound(this.getPosition(), SoundManager.HIT);
        }

        public override void VOnUpdate()
        {
            RegenerateHealth();

            if (!canFire)
            {
                //updates cooldwon timer 
                
                gunFireCooldownTimer += Game1.GetGameTime().ElapsedGameTime.Milliseconds;

                if (gunFireCooldownTimer >= gunFireRate)
                {
                    //If amount of time passed is greater than the gun's cooldown time, update flag to reflect that the actor
                    //is able to fire now and reset the cooldown timer 

                    canFire = true;
                    gunFireCooldownTimer = 0f;
                }
            }

            base.VOnUpdate();
        }

        //Data Members

        protected string unitType;                  //The unit's type
        protected AIState currentState;             //The actor's AI State
        protected UnitState unitState;              //The actor's state

        protected float currentHealth;              //The current health of the actor
        protected float maxHealth;                  //The maximum health of the actor
        protected float healthRegenRate;            //The rate of health regeneration

        protected bool user;                        //Whether the actor is user controlled
        protected bool player;                      //Whether the actor is part of the player's squad
        protected bool canFire;                     //Whether the actor can fire or not
        protected bool firing;                      //Whether the actor is firing 
        protected bool inCover;                     //Whether the actor is in cover
        protected bool crouching;                   //Whether the actor is crouching

        protected float gunRange;                  //Range of the actor's gun
        protected float gunFireRate;              //Measured in Milliseconds
        protected float gunFireCooldownTimer;     //Timer for cooldown

        protected int xp;                         //The actor's current XP

        //Added
        protected string modelName;
    }
}
